package com.he.service.trash;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.he.dao.TrashMapper;
import com.he.pojo.MailBox;
import com.he.pojo.Trash;
import com.he.pojo.TrashExample;
import com.he.service.mailbox.MailBoxServiceI;
/**
 * 
 * @类名：TrashServiceImpl
 * @描述：垃圾箱Service实现类
 * @日期：2018年8月18日 下午7:56:56
 */
@Service
public class TrashServiceImpl implements TrashServiceI {
	@Autowired
	TrashMapper trashMapper;
	@Resource
	MailBoxServiceI boxService;
	@Override
	public List<Trash> getPageTrashs(TrashExample trashExample, Trash trash) {
		//如果withPage属性值为1代表要分页
		if(trash.getWithPage()==1) {
			PageHelper.startPage(trash.getPageNo(),trash.getPageSize());
		}
		return trashMapper.selectByExample(trashExample);
	}
	
	@Override
	public List<Trash> getNoPageTrashs(TrashExample trashExample) {
		return trashMapper.selectByExample(trashExample);
	}
	
	@Override
	public Trash getTrashById(int id) {
		//通过id查询垃圾箱
		return trashMapper.selectByPrimaryKey(id);
	}

	@Override
	@Transactional
	public int insert(Trash trash,MailBox mailBox) {
		//仅插入不为空指针的垃圾箱数据
		trash.setReceiveId(mailBox.getReceiveId());
		trash.setReceiveName(mailBox.getReceiveName());
		trash.setSendId(mailBox.getMailId());
		trash.setSendName(mailBox.getSendName());
		trash.setMailTitle(mailBox.getMailTitle());
		trash.setMailDate(mailBox.getMailDate());
		trash.setMailId(mailBox.getMailId());
		boxService.delete(mailBox.getId());
		return trashMapper.insertSelective(trash);
	}

	@Override
	public int update(Trash trash) {
		//仅更新不为空指针的垃圾箱数据
		return trashMapper.updateByPrimaryKeySelective(trash);
	}

	@Override
	public int delete(int id) {
		//删除一条垃圾箱记录
		return trashMapper.deleteByPrimaryKey(id);
	}

}
